import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req, UseInterceptors, ClassSerializerInterceptor } from '@nestjs/common';
import { ProductService } from './product.service';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { SearchProduct } from './dto/search-product.dto';
import { PublicRoute } from 'src/decorations/PublicRoute';

@UseInterceptors(ClassSerializerInterceptor)
@ApiTags('商品')
@Controller('product')
export class ProductController {
  constructor(private readonly productService: ProductService) { }

  @ApiOperation({ summary: '添加商品' })
  @Post('add')
  create(@Body() createProductDto: CreateProductDto, @Req() request: any) {
    createProductDto.creatorGuid = request?.userinfo?.guid || ''
    return this.productService.create(createProductDto);
  }

  @ApiOperation({ summary: '查询所有商品' })
  @Post('findAll')
  findAll() {
    return this.productService.findAllByNoCondition();
  }

  @ApiOperation({ summary: '分页查询商品' })
  @Post('findAllByPage')
  findAllByPage(@Body() searchProduct: SearchProduct) {
    return this.productService.findAll(searchProduct);
  }

  @ApiOperation({ summary: 'PId查询商品' })
  @Get('findByPId')
  findOne(@Query('pId') pId: string) {
    return this.productService.findOne(pId) || {};
  }

  @ApiOperation({ summary: 'PId更新商品' })
  @Patch('update/:pId')
  update(@Param('pId') pId: string, @Body() updateProductDto: UpdateProductDto) {
    return this.productService.update(pId, updateProductDto);
  }

  @ApiOperation({ summary: 'PId删除商品' })
  @Delete('delete/:pId')
  remove(@Param('pId') pId: string) {
    return this.productService.remove(pId);
  }

  @ApiOperation({ summary: '获取分类' })
  @Get('category')
  getCategory() {
    return this.productService.getCategory();
  }

  @ApiOperation({ summary: '获取所有库存' })
  @Get('/all/stock')
  getAllProductStock() {
    return this.productService.getAllProductStock();
  }
}
